package com.jwthhealth_pub.old.constitution.view.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.app.App;
import com.jwthhealth_pub.old.api.ApiHelper;
import com.jwthhealth_pub.old.common.Constant;
import com.jwthhealth_pub.old.constitution.model.ConstitutionModel;
import com.jwthhealth_pub.old.constitution.view.ConstitutionAnswerActivity;
import com.jwthhealth_pub.old.constitution.view.QuestionnaireActivity;
import com.jwthhealth_pub.old.sign.model.LoginModel;
import com.jwthhealth_pub.old.signutil.model.db.UserInfoDao;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * Created by Administrator on 2016/12/14.
 */
public class QuestionnaireAdatper extends RecyclerView.Adapter {

    private Context context;
    private int type;
    private int gender;
    private List<Integer> answers;

    public List getAnswers() {
        return answers;
    }

    public QuestionnaireAdatper(Context context, int type, int gender) {
        this.context = context;
        this.type = type;
        this.gender = gender;

        if (type == 0 || type == 1 || type == 3 || type == 4) {
            answers = new ArrayList<>(8);
            for (int i=0; i<8; i++) {
                answers.add(i, 0);
            }
        } else if(type == 5) {
            answers = new ArrayList<>(6);
            for (int i=0; i<6; i++) {
                answers.add(i, 0);
            }
        } else {
            answers = new ArrayList<>(7);
            for (int i=0; i<7; i++) {
                answers.add(i, 0);
            }
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (viewType == ITEM_YTPE.ITEM_HEAD.ordinal()) {
            return new HeadViewHolder(LayoutInflater.from(context).inflate(R.layout.item_questionnaire_head, parent, false));
        } else if (viewType == ITEM_YTPE.ITEM_FOOT.ordinal()){
            return new FootViewHolder(LayoutInflater.from(context).inflate(R.layout.item_questionnaire_foot, parent, false));
        } else {
            return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_questionnaire, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (position == 0){
            HeadViewHolder headViewHolder = (HeadViewHolder) holder;
            switch (type){
                case 0:
                    headViewHolder.itemTitleTv.setText(context.getResources().getString(R.string.constitution_one));
                    break;
                case 1:
                    headViewHolder.itemTitleTv.setText(context.getResources().getString(R.string.constitution_two));
                    break;
                case 2:
                    headViewHolder.itemTitleTv.setText(context.getResources().getString(R.string.constitution_thr));
                    break;
                case 3:
                    headViewHolder.itemTitleTv.setText(context.getResources().getString(R.string.constitution_for));
                    break;
                case 4:
                    headViewHolder.itemTitleTv.setText(context.getResources().getString(R.string.constitution_fiv));
                    break;
                case 5:
                    headViewHolder.itemTitleTv.setText(context.getResources().getString(R.string.constitution_six));
                    break;
                case 6:
                    headViewHolder.itemTitleTv.setText(context.getResources().getString(R.string.constitution_sev));
                    break;
                case 7:
                    headViewHolder.itemTitleTv.setText(context.getResources().getString(R.string.constitution_eig));
                    break;
                case 8:
                    headViewHolder.itemTitleTv.setText(context.getResources().getString(R.string.constitution_nin));
                default:break;
            }
        } else if ((getItemCount() == 10 && position == 9) || (getItemCount() == 9 && position == 8) || (getItemCount() == 8 && position == 7)) {
            FootViewHolder footViewHolder = (FootViewHolder) holder;
            footViewHolder.commitBtn.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    // TODO: 2016/12/14 直接发请求
                    for (int i=0; i<answers.size(); i++) {
                        if (answers.get(i) == 0){
                            Toast.makeText(context, context.getResources().getString(R.string.physicalfiness_alert), Toast.LENGTH_SHORT).show();
                            return;
                        }
                    }
                    List answers = getAnswers();
                    String ans = "";
                    for (int i=0; i<answers.size(); i++){
                        Log.d("QuestionnaireAdatper", "answers.get(i):" + answers.get(i));
                        ans = ans + "," + answers.get(i);
                    }
                    ans = ans.substring(1);
                    LoginModel.DataBean userinfoModel = UserInfoDao.getInstance().queryUserinfoModel();
                    String id, androidtoken;
                    if (userinfoModel == null){
                        id = null;
                        androidtoken = null;
                    } else {
                        id = userinfoModel.getId();
                        androidtoken = userinfoModel.getAndroidtoken();
                    }
                    if (gender == 0){
                        gender = 2;
                    }
                    Call<ConstitutionModel> constitutionModelCall = ApiHelper.ConstitutionModelQuestion(id, String.valueOf(gender), App.preferenceUtil.getString(Constant.MAC_ADDRESS, ""), String.valueOf(type), ans, androidtoken);
                    constitutionModelCall.enqueue(new Callback<ConstitutionModel>() {
                        @Override
                        public void onResponse(Call<ConstitutionModel> call, Response<ConstitutionModel> response) {
                            ConstitutionModel body = response.body();
                            if (body.getCode().equals("0")) {
                                Intent intent = new Intent(context, ConstitutionAnswerActivity.class);
                                intent.putExtra(Constant.CONSTITUTION_TYPE, type);
                                Bundle bundle = new Bundle();
                                bundle.putSerializable(Constant.CONSTITUTION_DATA, body);
                                intent.putExtras(bundle);
                                context.startActivity(intent);
                                ((QuestionnaireActivity)context).finish();
                            }
                        }

                        @Override
                        public void onFailure(Call<ConstitutionModel> call, Throwable t) {
                            Log.d("QuestionnaireAdatper", "request fail");
                        }
                    });

                    ((QuestionnaireActivity)context).progressbar();
                }
            });
        }else {
            ViewHolder viewHolder = (ViewHolder) holder;
            //根据type判断章节，根据position判断题目
            if ((position -1) % 2 == 0){
                viewHolder.layoutBg.setBackgroundColor(context.getResources().getColor(R.color.questionnaire_item_bg));
            }
            String question = witchQuestion(type, position);
            viewHolder.itemTv.setText(question);
            viewHolder.itemRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup radioGroup, int i) {
                    switch (i) {
                        case R.id.item_opt_one:
                            answers.set(position-1, 1);
                            break;
                        case R.id.item_opt_two:
                            answers.set(position-1, 2);
                            break;
                        case R.id.item_opt_thi:
                            answers.set(position-1, 3);
                            break;
                        case R.id.item_opt_for:
                            answers.set(position-1, 4);
                            break;
                        case R.id.item_opt_fiv:
                            answers.set(position-1, 5);
                            break;
                        default:break;
                    }
                }
            });
        }
    }

    

    /**
     * 1245,7
     *
     * @return
     */
    @Override
    public int getItemCount() {
        int count = 0;

        if (type == 0 || type == 1 || type == 3 || type == 4) {
//            return 10;
            count = 10;
        } else if(type == 5){
            count = 8;
//            return 8;
        } else if (type == 2 || type == 6 || type == 7 || type == 8){
            count = 9;
        }

        return count;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0){
            return ITEM_YTPE.ITEM_HEAD.ordinal();
        } else if ((getItemCount() == 10 && position == 9) || (getItemCount() == 9 && position == 8) || (getItemCount() == 8 && position == 7)) {
            return ITEM_YTPE.ITEM_FOOT.ordinal();
        } else {
            return ITEM_YTPE.ITEM_BODY.ordinal();
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        RelativeLayout layoutBg;
        TextView itemTv;
        RadioGroup itemRg;

        public ViewHolder(View itemView) {
            super(itemView);
            itemTv = (TextView) itemView.findViewById(R.id.item_tv);
            itemRg = (RadioGroup) itemView.findViewById(R.id.item_rg);
            layoutBg = (RelativeLayout) itemView.findViewById(R.id.item_layout);
        }
    }

    class HeadViewHolder extends RecyclerView.ViewHolder{

        TextView itemTitleTv;

        public HeadViewHolder(View itemView) {
            super(itemView);
            itemTitleTv = (TextView) itemView.findViewById(R.id.tv_bold);
        }
    }

    class FootViewHolder extends RecyclerView.ViewHolder{

        Button commitBtn;

        public FootViewHolder(View itemView) {
            super(itemView);
            commitBtn = (Button) itemView.findViewById(R.id.btn_save);
        }
    }

    /**
     * 返回问题题目
     *
     * @param type
     * @param position
     */
    private String witchQuestion(int type, int position) {
        String text = null;
        position--;
        switch (type) {
            case 0:
                switch (position) {
                    case 0:
                        text = context.getResources().getString(R.string.questionnaire_one_one);
                        break;
                    case 1:
                        text = context.getResources().getString(R.string.questionnaire_one_two);
                        break;
                    case 2:
                        text = context.getResources().getString(R.string.questionnaire_one_thi);
                        break;
                    case 3:
                        text = context.getResources().getString(R.string.questionnaire_one_for);
                        break;
                    case 4:
                        text = context.getResources().getString(R.string.questionnaire_one_fiv);
                        break;
                    case 5:
                        text = context.getResources().getString(R.string.questionnaire_one_six);
                        break;
                    case 6:
                        text = context.getResources().getString(R.string.questionnaire_one_sev);
                        break;
                    case 7:
                        text = context.getResources().getString(R.string.questionnaire_one_eni);
                        break;
                    default:
                        break;
                }
                break;
            case 1:
                switch (position) {
                    case 0:
                        text = context.getResources().getString(R.string.questionnaire_two_one);
                        break;
                    case 1:
                        text = context.getResources().getString(R.string.questionnaire_two_two);
                        break;
                    case 2:
                        text = context.getResources().getString(R.string.questionnaire_two_thi);
                        break;
                    case 3:
                        text = context.getResources().getString(R.string.questionnaire_two_for);
                        break;
                    case 4:
                        text = context.getResources().getString(R.string.questionnaire_two_fiv);
                        break;
                    case 5:
                        text = context.getResources().getString(R.string.questionnaire_two_six);
                        break;
                    case 6:
                        text = context.getResources().getString(R.string.questionnaire_two_sev);
                        break;
                    case 7:
                        text = context.getResources().getString(R.string.questionnaire_two_eni);
                        break;
                    default:
                        break;
                }
                break;
            case 2:
                switch (position) {
                    case 0:
                        text = context.getResources().getString(R.string.questionnaire_thi_one);
                        break;
                    case 1:
                        text = context.getResources().getString(R.string.questionnaire_thi_two);
                        break;
                    case 2:
                        text = context.getResources().getString(R.string.questionnaire_thi_thi);
                        break;
                    case 3:
                        text = context.getResources().getString(R.string.questionnaire_thi_for);
                        break;
                    case 4:
                        text = context.getResources().getString(R.string.questionnaire_thi_fiv);
                        break;
                    case 5:
                        text = context.getResources().getString(R.string.questionnaire_thi_six);
                        break;
                    case 6:
                        text = context.getResources().getString(R.string.questionnaire_thi_sev);
                        break;
                    default:
                        break;
                }
                break;
            case 3:
                switch (position) {
                    case 0:
                        text = context.getResources().getString(R.string.questionnaire_for_one);
                        break;
                    case 1:
                        text = context.getResources().getString(R.string.questionnaire_for_two);
                        break;
                    case 2:
                        text = context.getResources().getString(R.string.questionnaire_for_thi);
                        break;
                    case 3:
                        text = context.getResources().getString(R.string.questionnaire_for_for);
                        break;
                    case 4:
                        text = context.getResources().getString(R.string.questionnaire_for_fiv);
                        break;
                    case 5:
                        text = context.getResources().getString(R.string.questionnaire_for_six);
                        break;
                    case 6:
                        text = context.getResources().getString(R.string.questionnaire_for_sev);
                        break;
                    case 7:
                        text = context.getResources().getString(R.string.questionnaire_for_eni);
                        break;
                    default:
                        break;
                }
                break;
            case 4:
                switch (position) {
                    case 0:
                        text = context.getResources().getString(R.string.questionnaire_fiv_one);
                        break;
                    case 1:
                        text = context.getResources().getString(R.string.questionnaire_fiv_two);
                        break;
                    case 2:
                        text = context.getResources().getString(R.string.questionnaire_fiv_thi);
                        break;
                    case 3:
                        text = context.getResources().getString(R.string.questionnaire_fiv_for);
                        break;
                    case 4:
                        text = context.getResources().getString(R.string.questionnaire_fiv_fiv);
                        break;
                    case 5:
                        text = context.getResources().getString(R.string.questionnaire_fiv_six);
                        break;
                    case 6:
                        text = context.getResources().getString(R.string.questionnaire_fiv_sev);
                        break;
                    case 7:
                        text = context.getResources().getString(R.string.questionnaire_fiv_eni);
                        break;
                    default:
                        break;
                }
                break;
            case 5:
                switch (position) {
                    case 0:
                        text = context.getResources().getString(R.string.questionnaire_six_one);
                        break;
                    case 1:
                        text = context.getResources().getString(R.string.questionnaire_six_two);
                        break;
                    case 2:
                        text = context.getResources().getString(R.string.questionnaire_six_thi);
                        break;
                    case 3:
                        text = context.getResources().getString(R.string.questionnaire_six_for);
                        break;
                    case 4:
                        text = context.getResources().getString(R.string.questionnaire_six_fiv);
                        break;
                    case 5:
                        //判断男女
                        if (gender == 0){
                            text = context.getResources().getString(R.string.questionnaire_six_sev);
                        } else if (gender == 1){
                            text = context.getResources().getString(R.string.questionnaire_six_six);
                        }
                        break;
                    default:
                        break;
                }
                break;
            case 6:
                switch (position) {
                    case 0:
                        text = context.getResources().getString(R.string.questionnaire_sev_one);
                        break;
                    case 1:
                        text = context.getResources().getString(R.string.questionnaire_sev_two);
                        break;
                    case 2:
                        text = context.getResources().getString(R.string.questionnaire_sev_thi);
                        break;
                    case 3:
                        text = context.getResources().getString(R.string.questionnaire_sev_for);
                        break;
                    case 4:
                        text = context.getResources().getString(R.string.questionnaire_sev_fiv);
                        break;
                    case 5:
                        text = context.getResources().getString(R.string.questionnaire_sev_six);
                        break;
                    case 6:
                        text = context.getResources().getString(R.string.questionnaire_sev_sev);
                        break;
                    default:
                        break;
                }
                break;
            case 7:
                switch (position) {
                    case 0:
                        text = context.getResources().getString(R.string.questionnaire_eni_one);
                        break;
                    case 1:
                        text = context.getResources().getString(R.string.questionnaire_eni_two);
                        break;
                    case 2:
                        text = context.getResources().getString(R.string.questionnaire_eni_thi);
                        break;
                    case 3:
                        text = context.getResources().getString(R.string.questionnaire_eni_for);
                        break;
                    case 4:
                        text = context.getResources().getString(R.string.questionnaire_eni_fiv);
                        break;
                    case 5:
                        text = context.getResources().getString(R.string.questionnaire_eni_six);
                        break;
                    case 6:
                        text = context.getResources().getString(R.string.questionnaire_eni_sev);
                        break;
                    default:
                        break;
                }
                break;
            case 8:
                switch (position) {
                    case 0:
                        text = context.getResources().getString(R.string.questionnaire_nin_one);
                        break;
                    case 1:
                        text = context.getResources().getString(R.string.questionnaire_nin_two);
                        break;
                    case 2:
                        text = context.getResources().getString(R.string.questionnaire_nin_thi);
                        break;
                    case 3:
                        text = context.getResources().getString(R.string.questionnaire_nin_for);
                        break;
                    case 4:
                        text = context.getResources().getString(R.string.questionnaire_nin_fiv);
                        break;
                    case 5:
                        text = context.getResources().getString(R.string.questionnaire_nin_six);
                        break;
                    case 6:
                        text = context.getResources().getString(R.string.questionnaire_nin_sev);
                        break;
                    default:
                        break;
                }
                break;
            default:
                break;
        }

        return text;
    }

    private enum ITEM_YTPE{
        ITEM_HEAD, ITEM_BODY, ITEM_FOOT;
    }
}
